
$(function() {
	$('.activeMenu').show();
	$('.simplemodal-container').css({
	    'height': '400px'
	});
	$('.simplemodal-wrap').css({
	    overflow: 'auto',
	    'overflow-x': 'hidden'
	});
	$('.onlyNumber').keydown(function(evt) {
		var charCode = (evt.which) ? evt.which : event.keyCode
		if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 96 || charCode > 105)) {
			return false;
		}
		return true;
	});
	$('.email').change(function() {
		var exr = /^[0-9a-z_\-\.]+@[0-9a-z\-\.]+\.[a-z]{2,4}$/i;
		$(this).removeClass("error");
		if (!exr.test($(this).val()) && $(this).val() != ""){
            $(this).addClass('error');
        }
	});
	
	$('#client').change(function() {
		$.ajax({
            'url' : url+'/sales/client/',
            'type' : 'POST',
            'dataType' : 'JSON',
            'data' : {
                'id' : $("#client option:selected").val()
            },
            'success' : function(res){
            	$('#client_ruc').val(res.ruc);
            	$('#client_address').val(res.address);
            }
        });
	});
	
	$('#type_document').change(function() {
		if ($('#type_document').val() == 1) {
			$('#divRuc').show();
			$('#client_ruc').addClass('noEmpty');
		} else {
			$('#divRuc').hide();
			$('#client_ruc').removeClass('noEmpty');
		}
	});
	
	$('#formAddClient').submit(function() {
		var result = true;
        $('#formAddClient .noEmpty').each(function(index, value) {
        	console.log($(this).val());
            $(this).removeClass("error");
            if ($.trim($(this).val()) == "") {
                $(this).addClass("error");
                result = result && false;
            }
        });
        console.log(result);
        var exr = /^[0-9a-z_\-\.]+@[0-9a-z\-\.]+\.[a-z]{2,4}$/i;
        $('#email').removeClass("error");
        if (!exr.test($('#email').val()) && $('#email').val() != ""){
            $('#email').addClass('error');
            result = result & false;
        } else {
            result = result & true;
        }
        if (result == false) {
            return false;
        }
	});
	
	$('.add-client').click(function (e) {
		$('#add-client-modal').modal();
		return false;
	});
	
	
	/////////////////////////////***para buscar productos mediante el popup que esta en guia de venta***////////////////
	

		$('.itemProducts').click(function() {
		var codeProduct = $(this).attr('codeProduct');
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/sales/stocks/',
            'type' : 'POST',
            'data' : {
                'code' : codeProduct,
                'description' : $(this).children('.descriptionProduct').text()
            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
	});
	
	$('.itemClients').click(function() {
		var codeClient = $(this).attr('codeClient');
		$('#add-contrato-model').html('');
		$.ajax({
            'url' : url+'/pago_mensual/contratosbyclient/',
            'type' : 'POST',
            'data' : {
                'id_cliente' : codeClient,
                'name' : $(this).children('.nameClient').text(),
				'document_client' : $(this).children('.document_client').text()
            },
            'success' : function(res){
            	$('#add-contrato-model').html(res);
            }
        });
	});




	$('#listProducts').submit(function() {
		var codeProduct = $('#code').val();
		var descriptionProduct = $('#description').val();
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/sales/products/',
            'type' : 'POST',
            'data' : {
                'code' : codeProduct,
                'description' : descriptionProduct
            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
		$('#add-product-model').modal();
		return false;
	});
	
	$('#listClients').submit(function() {
		var codeClient = $('#code').val();
		var name = $('#name').val();
		$('#add-contrato-model').html('');
		$.ajax({
            'url' : url+'/pago_mensual/clients/',
            'type' : 'POST',
            'data' : {
                'document_client' : codeClient,
                'name' : name
            },
            'success' : function(res){
            	$('#add-contrato-model').html(res);
            }
        });
		$('#add-contrato-model').modal();
		return false;
	});
	

		

	$('#listProducts_transfer').submit(function() {
		var codeProduct = $('#code').val();
		var descriptionProduct = $('#description').val();
		var id_warehouse_o = $('#id_warehouse_o').val();
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/warehouse_transfer/products/',
            'type' : 'POST',
            'data' : {
                'code' : codeProduct,
                'description' : descriptionProduct,
                'id_warehouse_o': id_warehouse_o
            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
		$('#add-product-model').modal();
		return false;
	});

	$('#listProducts_kardex').submit(function() {
		var codeProduct = $('#code').val();
		var descriptionProduct = $('#description').val();		
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/kardex/products/',
            'type' : 'POST',
            'data' : {
                'code' : codeProduct,
                'description' : descriptionProduct               
            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
		$('#add-product-model').modal();
		return false;
	});
	//************************************************************************//
	
	 $('.itemProduct').hover(function() {
		$(this).removeClass('tr_even');
		$(this).addClass('tr_odd');

	}, function() {
		$(this).addClass('tr_even');
		$(this).removeClass('tr_odd');
	});
	
	 $('.itemProduct_shop').hover(function() {
		$(this).removeClass('tr_even');
		$(this).addClass('tr_odd');
	}, function() {
		$(this).addClass('tr_even');
		$(this).removeClass('tr_odd');
	});
	
	 $('.itemProducts').hover(function() {
		$(this).removeClass('tr_even');
		$(this).addClass('tr_odd');
	}, function() {
		$(this).addClass('tr_even');
		$(this).removeClass('tr_odd');
	});
	
	$('.itemClients').hover(function() {
		$(this).removeClass('tr_even');
		$(this).addClass('tr_odd');
	}, function() {
		$(this).addClass('tr_even');
		$(this).removeClass('tr_odd');
	});
	
	$('.itemContratos').hover(function() {
		$(this).removeClass('tr_even');
		$(this).addClass('tr_odd');
	}, function() {
		$(this).addClass('tr_even');
		$(this).removeClass('tr_odd');
	});
	
	
	//*FUNCION QUE SE UTITLIZA AL HACERLE CLICK A LOS PRODUCTOS QUE SALEN EN 
	//LA GRILLA DEL POPUP DE BUSCAR AGREGAR PRODUCTOS EN EL GUIA DE VENTA *//



	$('.itemProductss').click(function() {

		var codeProduct = $(this).attr('codeProduct');
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/kardex_search/produs/',
            'type' : 'POST',
            'data' : {
            	'code' : codeProduct,
                
                'description' : $(this).children('.descriptionProduct').text()
            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
        $.modal.close();
	});








$('#listProductss').submit(function() {
		var codeProduct = $('#code').val();
		var descriptionProduct = $('#description').val();
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/warehouses_xproducts_search/products/',
            'type' : 'POST',
            'data' : {
                'code' : codeProduct,
                 'description': descriptionProduct,

            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
		$('#add-product-model').modal();
		return false;
	});
	







/* items products*/




/*
		$('.itemProductss').click(function() {
		var codeProduct = $(this).attr('codeProduct');
		
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/warehouses_xproducts_search/products/',
            'type' : 'POST',
            'data' : {
                'code' : codeProduct,
                'description' : $(this).children('.descriptionProduct').text()                
            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
	});



*/




	$('.itemProducts_transfer').click(function() {
		var codeProduct = $(this).attr('codeProduct');
		var id_warehouse_o = $('#id_warehouse_o').val();
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/warehouse_transfer/stocks/',
            'type' : 'POST',

            'data' : {

  				

                'code' : codeProduct,
                'description' : $(this).children('.descriptionProduct').text(),
                'id_warehouse_o': id_warehouse_o
            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
	});
	
		/*
	$('.abc').click(function() {
		$.modal.close();
	}*/



	
	//************************************************************
	
	
	$('.itemProduct_shop').click(function() {
		var codeProduct = $(this).attr('codeProduct');
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/shop/stocks/',
            'type' : 'POST',
            'data' : {
                'code' : codeProduct,
                'description' : $(this).children('.descriptionProduct').text()
            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
	});
	
	$('#listProducts_shop').submit(function() {
		var codeProduct = $('#code').val();
		var descriptionProduct = $('#description').val();
		$('#add-product-model').html('');
		$.ajax({
            'url' : url+'/shop/products/',
            'type' : 'POST',
            'data' : {
                'code' : codeProduct,
                'description' : descriptionProduct
            },
            'success' : function(res){
            	$('#add-product-model').html(res);
            }
        });
		$('#add-product-model').modal();
		return false;
	});
	
	
	
	
	
	
	
	
	$('.delProductList').click(function() {
		alert('eliminar');
	});
	
	
	
	
	//
	
	
	//$('#addProducts').click(function() {
		/* var isValid = true;
		var quantity = 0;
		var qboxes = 0;
		var qpackages = 0;
		var qunits = 0;
		var inputHtml = '';
		$('.mountProduct').each(function() {
			if (parseInt($(this).val()) > parseInt($(this).attr('rel'))) {
				$(this).addClass("error");
				isValid = false;
			} else {
				inputHtml += '<input type="hidden" name="product['+$('#dataProduct').attr('code')+']['+$(this).attr('stock')+']" value="'+$(this).val()+'" />';
				quantity += parseInt($(this).val());
				qboxes += parseInt($(this).attr('cantBoxes'));
				qpackages += parseInt($(this).attr('cantPackages'));
				qunits += parseInt($(this).attr('cantUns'));
			}
		});		
		inputHtml += '<input type="hidden" name="amount" value="'+$('#amount').val()+'" />';
		if (isValid && quantity > 0) {
			var rowElement = $('#rowListProductBlank').clone(true, true);
			rowElement.children('.inputProduct').append(inputHtml);
			rowElement.children('.codeProduct').html($('#dataProduct').attr('code'));
			rowElement.children('.descriptionProduct').html($('#dataProduct').html());
			rowElement.children('.quantityBoxesProduct').html(qboxes);
			rowElement.children('.quantityPackagesProduct').html(qpackages);
			rowElement.children('.quantityUnitsProduct').html(qunits);
			rowElement.children('.quantityProduct').html(quantity);
			// rowElement.children('.priceProduct').html('S/. ' + parseInt($('#dataProduct').attr('price'))*quantity);
			rowElement.children('.priceProduct').html( /*'S/. ' + $('#amount').val());
			/* $('#contentProductList').append('<tr>'+rowElement.html()+'</tr>'); */
			/* $.modal.close() */
		/*}*/
	//});
	
	$('.closeModel').click(function() {
		$.modal.close()
	});
	
	$('#formAddPurchase').submit(function() {
		/* var result = true;
		$('#formAddPurchase .noEmpty').each(function(index, value) {
            $(this).removeClass("error");
            if ($.trim($(this).val()) == "") {
                $(this).addClass("error");
                result = result & false;
            }
        });
		if ($('#contentProductList .elementList').size() == 0) {
			result = result & false;
			$('#contentMessageErrorFormAddPurchase').text('No se agregaron productos');
			$('#message_error_formAddPurchase').removeClass('hidden');
			$('#message_error_formAddPurchase').show();
		}
		if (result == false) {
			return false;
		} */
	});
		var result = true;
	if ($('#formAddProducts').length ) {
		$('#code_product').focusout(function() {
			var codeProduct = $('#code_product').val();
			$.ajax({
	            'url' : url+'/products/evalue/',
	            'type' : 'POST',
	            'data' : {
	                'code' : codeProduct
	            },
	            'success' : function(res){
	            	$('#code_product').removeClass("error");
	            	if (res != "false") {
	            		$('#code_product').addClass("error");
	            	}
	            }
	        });
		});
		
		$('#formAddProducts').submit(function (){
			var result = true;
	        $('.noEmpty').each(function(index, value) {
	            $(this).removeClass("error");
	            if ($.trim($(this).val()) == "") {
	                $(this).addClass("error");
	                result = result & false;
	            }
	        });
	        
	        if (result == false) {
	            return false;
	        }
		});
	}

	if ($('#formAddGroups').length ) {
		$('#description').focusout(function() {
			var description = $('#description').val();
			$.ajax({
				'url' : url+'/category_products/evalue',
				'type' : 'POST',
				'data' : {
					'description' : description
				},
				'success' : function(res){
					$('#description').removeClass("error");
					if (res != "false") {
						$('#description').addClass("error");
					}
				}
			});
		});
	}

	$('#formAddGroups').submit(function (){
		var result = true;
		$('.noEmpty').each(function(index, value) {
			$(this).removeClass("error");
			if ($.trim($(this).val()) == "") {
				$(this).addClass("error");
				result = result & false;
			}
		});

		if (result == false) {
			return false;
		}
	});
	
});

 function confirmar_borrar(url){
    if(confirm('Desea eliminar?')) location.href = url;
} 

 

$(document).ready(function(){
	$('#iniciar').click(function(){
            var user = $('input[name=user]').val();
            var pass = $('input[name=pass]').val();
	   
           alert('hola');
	})
})